//
//  WCNJoinMeetingVC.m
//  WCN
//
//  Created by wsj2015 on 16/5/25.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNJoinMeetingVC.h"
#import "WSJTexfildForSuggestionList.h"
#import "WCBorderView.h"
#import <ZhumuSDK/ZhumuSDK.h>
#import "CallCenter.h"
#import "SettingsViewController.h"
#import "UIImage+Additions.h"
#import "UIColor+Additions.h"
#import "MBProgressHUD.h"
#import "WCButton.h"
#import "UIKit+AFNetworking.h"
#import "JCAlertView.h"
#import "WCNZhumuManager.h"
#import "WCNAppDelegate.h"

@interface WCNJoinMeetingVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet WSJTexfildForSuggestionList *meetingTF;
@property (weak, nonatomic) IBOutlet WCBorderView *tfBgview;

@property (weak, nonatomic) IBOutlet UILabel *myNameLabel;

@end

@implementation WCNJoinMeetingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self config];
    
}


- (void)config{
    [[ZhumuSDK sharedSDK] setZhumuRootController:self.navigationController];
    [self configUI];
    self.meetingTF.delegate = self;
    self.meetingTF.text = _meetingID;
    self.myNameLabel.text = [[[WCDefaultManager shareMg] account] name];
    self.meetingTF.textAlignment= NSTextAlignmentCenter;
  
}

- (void)showSuggestList{
    if ([[WCDefaultManager shareMg]getMeetingAccounts].count>0 ) {
        self.meetingTF.ableSuggestinglist = YES;
        self.meetingTF.superView = self.view;
        CGRect  frame = self.tfBgview.frame;
        self.meetingTF.customFrame = CGRectMake(frame.origin.x+4, CGRectGetMaxY(frame), kScreenWidth , 100);
        self.meetingTF.suggestionArray =[[WCDefaultManager shareMg]getMeetingAccounts];
        weakSelf_define;
        self.meetingTF.callBack = ^(NSString *phone){
            weakSelf.meetingTF.text =phone;
        };
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)configUI{
    self.title = @"加入会议";
    self.view.backgroundColor = kAppBg;
}
#pragma mark - UITextFieldDelegate

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.meetingTF hideList];
    
    [self.view endEditing:YES];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
   
        [self showSuggestList];
    
}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    if (self.meetingID) {
        self.meetingTF.text = self.meetingID;
    }
    WCNAppDelegate *delegate = (WCNAppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.isMeetingNow = NO;
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}
- (void)setMeetingID:(NSString *)meetingID{
    _meetingID = meetingID;
    if (self.meetingTF) {
        self.meetingTF.text = meetingID;
    }
}
- (IBAction)joinAction:(id)sender {
    [self.view endEditing:YES];
    if (!([self.meetingTF.text length]>0)) {
        WSJSHOW(@"请输入会议号～");
        return;
    }
    [self observeNetworkStatus];
}

- (void)joinMeeting {
    WCNAppDelegate *delegate = (WCNAppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.isMeetingNow = YES;
    [[WCNZhumuManager shareManager] joinMeeting:self.meetingTF.text vc:self];
}
- (void)pageBack {
    WCNAppDelegate *delegate = (WCNAppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.isMeetingNow = NO;
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
    [super pageBack];
    
}
- (void)observeNetworkStatus{
    
    WCNetworkAccessManager *netWork = [WCNetworkAccessManager sharedMg];
    if (netWork.netStatus == NetworkStatusWIFI) {
            [self joinMeeting];
    } else {
        if (netWork.netStatus == NetworkStatusUnknow || netWork.netStatus == NetworkStatusNO) {
            
           [JCAlertView showOneButtonWithTitle:nil Message:@"你现在的网络不可用，无法进行视频会议" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:nil];
            
        } else {
           [self alertMessage:@"你现在处于非wifi网络下，是否确定进行视频会议"];
        }
    }
}

//提示当前网络是否wifi
- (void)alertMessage:(NSString *)message {
    
    [JCAlertView showTwoButtonsWithTitle:nil Message:message ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
  
    [self joinMeeting];

    } ButtonType:JCAlertViewButtonTypeCancel ButtonTitle:@"取消" Click:nil];
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return toInterfaceOrientation != UIDeviceOrientationPortraitUpsideDown;
}

- (BOOL)shouldAutorotate
{
//    if ([self.topViewController isKindOfClass:[AddMovieViewController class]]) { // 如果是这个 vc 则支持自动旋转
        return YES;
//    }
//    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

@end
